<?php 
class fileMngActions extends sfActions
{
		public function executeDownloadFile($request)
		{
       	// Enforce full download and prevent caching
                    session_cache_limiter('none');
                    // No layout needed for file downloads
                    $this->setLayout(false);
                    $origin = $request->getParameter('origin', false);
                    $path_to_file = sfConfig::get('sf_' .  $origin  .'_dir').DIRECTORY_SEPARATOR.$request->getParameter('name', false);
                    //$path_to_file = 'C:\workspace\sfprojects\e-dsl\web\uploads\work_2009.doc';
                    sfConfig::set('sf_web_debug', false);
		            // Renommage du fichier
                    $path_parts = pathinfo($path_to_file);
                    $nomdufichier = $request->getParameter('description', false);
                    
                    if($nomdufichier=='')
                    {
                    	$nomdufichier = $path_parts['filename'];
                   	}
                    
                    $funcs = new erdfFuncs;
					$nomdufichier = $funcs->stripAccents(utf8_decode($nomdufichier));
					$nomdufichier = strtolower($nomdufichier);
					$nomdufichier = str_replace(' ', '_', $nomdufichier);
					$nomdufichier = str_replace("'", "", $nomdufichier);
                    
                    // GERE LES EXTENSIONS DES FICHIERS
					$extension_fichier = $path_parts['extension'];
					
					switch($extension_fichier)
					{
						case 'doc' : $fileType = 'application/word';
						break;
												
						case 'xls' : $fileType = 'application/excel';
						break;
						
						default : $fileType = 'application/octet-stream';
						break;
					}
                    // Prepare http headers
                    //$fileType = 'application/word';//$file->getFormat()->getNazwa(); //this is my file type from db - for example application/pdf
                    $response = $this->getResponse();
                    $response->clearHttpHeaders();
                    $response->setHttpheader('Pragma: public', true);
                    $response->addCacheControlHttpHeader('Cache-Control', 'must-revalidate');
                    $response->setHttpHeader('Expires', 'Sat, 26 Jul1997 05:00:00 GMT');
                    $response->setHttpHeader("Last-Modified", gmdate("D, d M Y H:i:s") . " GMT");
                    $response->setContentType($fileType, true);
                    $response->setHttpHeader('Content-Description','File Transfer');
                    $response->setHttpHeader('Content-Transfer-Encoding', 'binary', true);
                    $response->setHttpHeader('Content-Length', filesize('' . (string)utf8_decode($path_to_file. '')));
                    $response->setHttpHeader('Content-Disposition','attachment; filename=' . $nomdufichier . '.' . $extension_fichier);

                    // Unlock session in order to prevent php sessionwarnings
                    $this->getUser()->shutdown();
                    // Send http headers to user client
                    $response->sendHttpHeaders();
                    // Send file to user client
                    $response->setContent(file_get_contents($path_to_file));
                    // $response->sendContent();
                    // Exit without a template
                    return sfView::NONE;		
		}

	/**
	 * Looks up the passed file id in the database,
	 * fetches the corresponding file from the file system
	 * and finally returns it to the users' browser.
	 *
	 * Or in short words: Implements file download
	 *
	 * @param sfRequest $request A request object
	 */
	public function executeDownloadFileFromDb($request) {
		
		$this->request = $request;
		$this->fileId = $request->getParameter('id', false);
		$this->fileId = 'C:\workspace\sfprojects\e-dsl\web\uploads\work_2009.ddd';
		// fileId was passed - validate it
		if ($this->fileId ) {

			// Validate fileId in db
			//file = FilesPeer::retrieveByPK($this->fileId);

			// Passed clickedId is invalid -
			// exit immediately!
/*			if (count($file) != 1) {
				return sfView::NONE;

			// Passed clickedId is valid - go on and fetch files
			} else {*/
				
				if (is_file(utf8_decode($this->fileId))) {

					// Enforce full download and prevent caching
					session_cache_limiter('none');
					
					// No layout needed for file downloads
					$this->setLayout(false);
					
					// Prepare http headers
					$fileType = 'doc';
					$response = $this->getResponse();
					$response->clearHttpHeaders();
					$response->setHttpheader('Pragma: public', true);	
					$response->addCacheControlHttpHeader('Cache-Control', 'must-revalidate');
					$response->setHttpHeader('Expires', 'Sat, 26 Jul 1997 05:00:00 GMT');
					$response->setHttpHeader("Last-Modified" . gmdate("D, d M Y H:i:s") . " GMT");
					$response->setContentType('application/octet-stream', true);								
					$response->setHttpHeader('Content-Description', 'File Transfer');
					$response->setHttpHeader('Content-Transfer-Encoding', 'binary', true);
					$response->setHttpHeader('Content-Length', filesize('' . (string)utf8_decode($this->fileId . '')));
					$response->setHttpHeader('Content-Disposition', 'attachment; filename=' . str_replace(" ", "_", utf8_decode($this->fileId)) . $fileType); 				
										
					// Make entry in dowload stats
					/*$downloadsObj = new Downloads();
					$downloadsObj->setFilesId($file->getId());			
					$downloadsObj->setUserip($this->getUserIP());				
					if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) $downloadsObj->setUserproxy($_SERVER["REMOTE_ADDR"]);
					$downloadsObj->setUseragent($_SERVER['HTTP_USER_AGENT']);
					$downloadsObj->setDownloadedAt(time());							
					$downloadsObj->save();
					*/
					// Unlock session in order to prevent php session warnings
					//$this->getUser()->shutdown();
					
					// Send http headers to user client
					$response->sendHttpHeaders();
					
					// Send file to user client
					$response->setContent(file_get_contents(utf8_decode($this->fileId)));
					$response->sendContent();		
					
					// Exit without a template
					return sfView::NONE;				
					
				/*} else {
					
					return sfView::NONE;	
									
				} // end if-else				
				*/
			} // end if-else

		// fileId was not passed
		} else {
			return sfView::NONE;
		} // end if-else
		
	} // end function 'executeDownloadFile'

	
	
}
?>